package OrderService

import (
	"QingDai-Gin/api/models"
	"QingDai-Gin/config"
	"gorm.io/gorm"
)

func GetAllOrder() ([]models.Order, *gorm.DB) {
	var orders []models.Order
	Mysql := config.Mysql()
	tx := Mysql.Table("order__order").Find(&orders)
	return orders, tx
}

func GetOrderById(id int) (models.Order, *gorm.DB) {
	var order models.Order
	Mysql := config.Mysql()
	tx := Mysql.Table("order__order").First(&order, id)
	return order, tx
}

func GetOrdersByP(p map[string]any) ([]models.Order, *gorm.DB) {
	var orders []models.Order
	Mysql := config.Mysql()
	tx := Mysql.Table("order__order")
	for key, value := range p {
		tx.Where(key+" = ?", value)
	}
	tx.Find(&orders)
	return orders, tx
}
func GetOrderByP(p map[string]any) (models.Order, *gorm.DB) {
	var order models.Order
	Mysql := config.Mysql()
	tx := Mysql.Table("order__order")
	for key, value := range p {
		tx.Where(key+" = ?", value)
	}
	tx.First(&order)
	return order, tx
}

func CreateOrder(order models.Order) {
	Mysql := config.Mysql()
	Mysql.Table("order__order").Create(order)
}

func UpdateOrder(order models.Order) {
	Mysql := config.Mysql()
	Mysql.Table("order__order").Save(order)
}

func DeleteOrder(order models.Order) {
	Mysql := config.Mysql()
	Mysql.Table("order__order").Delete(order)
}
